<template>
  <div class="containerdiv">
    <el-container>
      <el-aside width="240px">
        <SideBar :toggle="isCollapse" :menus="menus" @menuSelect="onMenuSelect(arguments)"></SideBar>
      </el-aside>
      <el-container>
        <el-main>
          <HeadNav @click="onDrawerBtClick" style="height:40px;"></HeadNav>
          <HeadTab ref="headTab" :tabs="navTitles"></HeadTab>

          <!-- 路由出口 -->
          <!-- 路由匹配到的组件将渲染在这里 -->
          <transition name="slide-fade" mode="out-in">
            <router-view></router-view>
          </transition>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script>
import SideBar from "./NavSideBar";
import HeadNav from "./HeadNav";
import HeadTab from "./HeadTab";
export default {
  components: {
    SideBar,
    HeadNav,
    HeadTab
  },
  data() {
    return {
      isCollapse: false,
      navTitles: [],
      menus: []
    };
  },
  created() {
    

    this.menus = [
      {
        hasSubMenu: false,
        index: "1",
        title: "首页1",
        icon: "el-icon-location",
        closable: false,
        url: "/container"
      },
      {
        hasSubMenu: false,
        index: "4",
        title: "首页2",
        icon: "el-icon-location",
        closable: true,
        url: "/tab2"
      },
      {
        hasSubMenu: false,
        index: "5",
        title: "首页3",
        icon: "el-icon-location",
        closable: true,
        url: "/htmlTab"
      },
      {
        hasSubMenu: true,
        index: "6",
        title: "导航1",
        icon: "el-icon-location",
        child: [
          {
            hasSubMenu: false,
            index: "4-1",
            title: "导航1.1",
            closable: true,
            url: "/container"
          },
          {
            hasSubMenu: true,
            index: "4-2",
            title: "导航1.2",
            child: [
              {
                hasSubMenu: false,
                index: "4-2-1",
                title: "导航1.2.1",
                closable: true,
                url: "/container"
              },
              {
                hasSubMenu: false,
                index: "4-2-2",
                title: "导航1.2.2",
                closable: true,
                url: "/container"
              }
            ]
          }
        ]
      }
    ];
    turnNavMenuArr2Map(this.menus, __navMap);
    var firstPage=this.menus[0];
    this.navTitles = [firstPage];
  },
  methods: {
    handleOpen(key, keyPath) {
      console.log(key, keyPath);
    },
    handleClose(key, keyPath) {
      console.log(key, keyPath);
    },
    onDrawerBtClick() {
      // alert("qwe");
      this.isCollapse = !this.isCollapse;
    },
    onMenuSelect(args) {
      let selectIndex = args[0];
      let indexArray = args[1];

      let targetItem = __navMap.get(selectIndex);
      this.$refs.headTab.jump2Tab(targetItem);
    }
  }
};

let __navMap = new Map();
function turnNavMenuArr2Map(srcArray) {
  for (let itemIndex in srcArray) {
    var item = srcArray[itemIndex];

    if (item.hasSubMenu === false) {
      __navMap.set(item.index, item);
    } else {
      turnNavMenuArr2Map(item.child);
    }
  }
}
</script>

<style lang="scss" scoped>
/* 可以设置不同的进入和离开动画 */
/* 设置持续时间和动画函数 */
.slide-fade-enter-active {
  transition: all 0.3s ease;
}
.slide-fade-leave-active {
  transition: all 0.3s cubic-bezier(1, 0.5, 0.8, 1);
}
.slide-fade-enter, .slide-fade-leave-to
/* .slide-fade-leave-active for below version 2.1.8 */ {
  transform: translateX(10px);
  opacity: 0;
}

.containerdiv {
  position: absolute;
  left: 0;
  right: 0;
  top: 0;
  bottom: 0;
  background-color: red;
}
.el-header {
  background-color: #b3c0d1;
  color: #333;
  height: 50px;
  line-height: 50px;
}

.el-aside {
  background-color: #d3dce6;
  color: #333;
  padding: 0;
  margin: 0;
  height: 1000px;
}

.el-main {
  padding: 0;
  margin: 0;
  background-color: #ffffff;
  color: #333;
  text-align: center;
}
</style>